/*
 * File: SendMessages.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets.cron;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import faceBook.Message;
import faceBook.User;
import foxtrotbravo.DataConnection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * The Class SendMessages.
 */
public class SendMessages extends HttpServlet
{

  /*
   * (non-Javadoc)
   * 
   * @see
   * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  @SuppressWarnings("unused")
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
  {
    GregorianCalendar today = new GregorianCalendar();
    createDefaultMessages(today);
    ArrayList<Message> messages = Message.getAllMessageForDay(today);
    for (Message m : messages)
    {
      m.postMessage();
    }
  }

  /**
   * Creates the default messages.
   * 
   * @param day
   *          the day
   */
  public static void createDefaultMessages(GregorianCalendar day)
  {
    try
    {
      if (DataConnection.connect())
      {
        String month = String.valueOf(day.get(Calendar.MONTH) + 1);
        String date = String.valueOf(day.get(Calendar.DATE));
        String statement = "select f.user, f.friend, "
            + "u.defaultBDMessageMale, u.defaultBDMessageFemale, "
            + "(select gender from user where id = f.friend) as gender "
            + "from friends f, user u where "
            + "u.useDefaultBD = true and f.user = u.id and "
            + "(select date_format(birthday, '%c-%e') from user where id = f.friend) = '|month|-|date|' and "
            // There is not already a birthday message for this user today
            + "(select count(id) from message m, message_friend mf where m.type='birthday' and m.user = u.id and mf.message = m.id and mf.friend = f.friend and date_format(m.sendDate, '%c-%e') = '|month|-|date|') = 0";
        statement = statement.replaceAll("\\|month\\|", month).replaceAll(
            "\\|date\\|", date);
        PreparedStatement stmt = DataConnection.getConnection()
            .prepareStatement(statement);
        ResultSet results = stmt.executeQuery();
        while (results.next())
        {
          Message message = new Message();
          message.setDefault(true);//Makes it free
          message.setUser(new User(results.getLong("user")));
          User friend = new User(results.getLong("friend"));
          message.addFriend(friend);
          message.setType("birthday");
          message.setSendDate(new Date().getTime());
          if ("F".equals(results.getString("gender")))
          {
            message.setMessage(friend.replaceString(results.getString("defaultBDMessageFemale")));
          } else
          {
            message.setMessage(friend.replaceString(results.getString("defaultBDMessageMale")));
          }
          message.save();
        }
      }
    } catch (SQLException e)
    {
      e.printStackTrace();
    }
  }

}
